package com.wendy.web.editor;

import com.wendy.utils.Constants;
import com.wendy.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.PropertiesEditor;

/**
 * @author zhuwending
 */
public class DateEditor extends PropertiesEditor {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isNotBlank(text) && Constants.NULL.equalsIgnoreCase(text)) {
            if (text.length() == Constants.YYYY_MM_DD_HH_MM_SS.length()) {
                this.setValue(DateUtil.convert2Date(text, Constants.YYYY_MM_DD_HH_MM_SS));
            } else if (text.length() == Constants.YYYY_MM_DD.length()) {
                this.setValue(DateUtil.convert2Date(text, Constants.YYYY_MM_DD));
            } else if (text.length() == Constants.YYYY_MM_DD_HH_MM.length()) {
                this.setValue(DateUtil.convert2Date(text, Constants.YYYY_MM_DD_HH_MM));
            }
        } else {
            this.setValue(null);
        }
    }
}
